/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderStatusPanel.java
 *
 * Created on Mar 31, 2010, 4:29:26 PM
 */

package edu.asu.cse360.client.ui.frame;

import edu.asu.cse360.client.model.ColumnModel;
import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.util.Formatter;
import edu.asu.cse360.client.util.OrderStatusListener;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.model.Employee;
import edu.asu.cse360.model.IngredientQuantity;
import edu.asu.cse360.model.ItemOrder;
import edu.asu.cse360.model.Order;
import edu.asu.cse360.model.OrderCustomization;
import edu.asu.cse360.model.OrderStatus;
import edu.asu.cse360.service.ItemOrderService;
import edu.asu.cse360.service.OrderStatusService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *This class is to display the order status for the customer
 * @author ggreiner
 */
public class OrderStatusPanel extends javax.swing.JPanel {

    private ItemOrderTableModel tableModel;
    private ItemOrder selectedItemOrder;
    private ComboBoxModel statusModel;
    private ComboBoxModel saltModel;
    private ComboBoxModel sugarModel;
    private ItemOrderService itemOrderSvc;
    private OrderStatusService orderStatusSvc;
    private Employee loggedInUser;
    private List<OrderStatusListener> listeners;

    private static final ColumnModel<ItemOrder> employeeColumnModel = new ColumnModel<ItemOrder>(){
        private String[] columns = {"Id","Menu Item", "Status", "Order Time", "Table #"};
        public Object getValue(ItemOrder item, int index) {
            Object value = null;
            switch(index){
                  case 0:
                      value = item.getId();
                      break;
                  case 1:
                      value = item.getMenuItem().getName();
                      break;
                  case 2:
                      value = item.getStatus();
                      break;
                  case 3:
                      value = item.getOrderTime();
                      break;
                  case 4:
                      value = item.getCustomerSession().getTable().getTableNumber();
                      break;
              }
            return value;
        }

        public String getColumnName(int index) {
            return columns[index];
        }

        public int getCount() {
            return columns.length;
        }

    };

    private static final ColumnModel<ItemOrder> customerColumnModel = new ColumnModel<ItemOrder>(){
        private String[] columns = {"Menu Item", "Status", "Order Time"};
        public Object getValue(ItemOrder item, int index) {
            Object value = null;
            switch(index){
                  case 0:
                      value = item.getMenuItem().getName();
                      break;
                  case 1:
                      value = item.getStatus();
                      break;
                  case 2:
                      value = item.getOrderTime();
                      break;
              }
            return value;
        }

        public String getColumnName(int index) {
            return columns[index];
        }

        public int getCount() {
            return columns.length;
        }

    };


    /** Creates new form OrderStatusPanel */
    public OrderStatusPanel() {
        listeners = new ArrayList();
        loggedInUser = RestaurantContext.getLoggedInUser();
        itemOrderSvc = ServiceFactory.getItemOrderService();
        orderStatusSvc = ServiceFactory.getOrderStatusService();
        //
        List<OrderStatus> availableStatuses;
        ColumnModel<ItemOrder> columns;
        if(RestaurantContext.isCustomerMode()){
            availableStatuses = Arrays.asList(new OrderStatus[]{OrderStatus.CANCELLED});
            columns = customerColumnModel;
        }else{
            availableStatuses = orderStatusSvc.getEditableStatuses(loggedInUser);
            columns = employeeColumnModel;
        }

        statusModel = new DefaultComboBoxModel(availableStatuses.toArray());
        saltModel = new DefaultComboBoxModel(IngredientQuantity.values());
        sugarModel = new DefaultComboBoxModel(IngredientQuantity.values());
        //

        tableModel = new ItemOrderTableModel(columns);
        if(!RestaurantContext.isCustomerMode()){
            refreshTableModel();
        }
        initComponents();
        if(!tableModel.getItemOrders().isEmpty()){
            updateSelectedItem(0);
        }
        if(RestaurantContext.isCustomerMode()){
            idTxt.setVisible(false);
            chefTxt.setVisible(false);
            saltComboBox.setEnabled(true);
            sugarComboBox.setEnabled(true);
            notesTxt.setEnabled(true);
        }
    }

    public void setOrder(Order order){
        List<ItemOrder> itemOrders = order.getItemOrders();
        tableModel.setItemOrders(itemOrders);
    }

    public void addOrderStatusListener(OrderStatusListener listen)
    {
        listeners.add(listen);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        idTxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        statusBox = new javax.swing.JComboBox();
        menuItemTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        orderTimeTxt = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        waitPersonTxt = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        chefTxt = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        updateOrderBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        notesTxt = new javax.swing.JTextArea();
        saltComboBox = new javax.swing.JComboBox();
        sugarComboBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jTable1.setModel(getTableModel());
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Order Id:");

        idTxt.setEnabled(false);

        jLabel2.setText("Menu Item:");

        jLabel3.setText("Status:");

        statusBox.setModel(statusModel);

        menuItemTxt.setEnabled(false);

        jLabel4.setText("Order Time:");

        orderTimeTxt.setEnabled(false);

        jLabel5.setText("Wait Person:");

        waitPersonTxt.setEnabled(false);

        jLabel6.setText("Chef:");

        chefTxt.setEnabled(false);

        jLabel7.setText("Salt Quantity:");

        jLabel8.setText("Sugar Quantity:");

        jLabel9.setText("Notes:");

        updateOrderBtn.setLabel("Update Order");
        updateOrderBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateOrderBtnActionPerformed(evt);
            }
        });

        notesTxt.setColumns(20);
        notesTxt.setRows(5);
        notesTxt.setEnabled(false);
        jScrollPane2.setViewportView(notesTxt);

        saltComboBox.setModel(saltModel);
        saltComboBox.setEnabled(false);
        saltComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saltComboBoxActionPerformed(evt);
            }
        });

        sugarComboBox.setModel(sugarModel);
        sugarComboBox.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jLabel1)
                    .add(jLabel6)
                    .add(jLabel5)
                    .add(jLabel7)
                    .add(jLabel8)
                    .add(jLabel4)
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel3)
                        .add(jLabel9)))
                .add(35, 35, 35)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(sugarComboBox, 0, 244, Short.MAX_VALUE)
                    .add(saltComboBox, 0, 244, Short.MAX_VALUE)
                    .add(orderTimeTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .add(waitPersonTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .add(chefTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .add(menuItemTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .add(idTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .add(statusBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 236, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, updateOrderBtn)
                    .add(jScrollPane2))
                .add(274, 274, 274))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(idTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(menuItemTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(orderTimeTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(waitPersonTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(chefTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(saltComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(sugarComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel9)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusBox)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(updateOrderBtn)
                .addContainerGap())
        );

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 426, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jPanel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        handleTableSelection();
    }//GEN-LAST:event_jTable1MouseClicked

    private void updateOrderBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateOrderBtnActionPerformed
        handleUpdateOrder();
    }//GEN-LAST:event_updateOrderBtnActionPerformed

    private void saltComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saltComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_saltComboBoxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        refreshTableModel();
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField chefTxt;
    private javax.swing.JTextField idTxt;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField menuItemTxt;
    private javax.swing.JTextArea notesTxt;
    private javax.swing.JTextField orderTimeTxt;
    private javax.swing.JComboBox saltComboBox;
    private javax.swing.JComboBox statusBox;
    private javax.swing.JComboBox sugarComboBox;
    private javax.swing.JButton updateOrderBtn;
    private javax.swing.JTextField waitPersonTxt;
    // End of variables declaration//GEN-END:variables

    private TableModel getTableModel(){
        return tableModel;
    }

    private void handleTableSelection() {
        updateSelectedItem(null);

    }

    private void updateSelectedItem(Integer index) {
        if(index == null){
            updateSelectedItem();
        }else{
            selectedItemOrder = tableModel.getItemAtIndex(0);
        }
        if(RestaurantContext.isCustomerMode()){
            if(selectedItemOrder.getStatus() == OrderStatus.CANCELLED){
                 statusModel = new DefaultComboBoxModel(new OrderStatus[]{selectedItemOrder.getStatus()});
            }else{
                 statusModel = new DefaultComboBoxModel(new OrderStatus[]{selectedItemOrder.getStatus(), OrderStatus.CANCELLED});
            }
            statusBox.setModel(statusModel);
        }
        updateItemOrderFields();
    }

    private ItemOrder getSelectedItemOrder() {
        int selectedIndex = jTable1.getSelectedRow();
        return tableModel.getItemAtIndex(selectedIndex);
    }

    private void updateItemOrderFields() {
        if(selectedItemOrder == null){
            clearItemOrderFields();
        }else{
            statusBox.setEnabled(true);
            idTxt.setText(Formatter.format(selectedItemOrder.getId().toString()));
            menuItemTxt.setText(Formatter.format(selectedItemOrder.getMenuItem().getName()));
            orderTimeTxt.setText(Formatter.format(selectedItemOrder.getOrderTime()));
            setEmployeeTxt(selectedItemOrder.getWaitPerson(), waitPersonTxt);
            setEmployeeTxt(selectedItemOrder.getChef(), chefTxt);
            if(selectedItemOrder.getCustomization() != null){
                saltComboBox.setSelectedItem(selectedItemOrder.getCustomization().getSaltQuantity());
                sugarComboBox.setSelectedItem(selectedItemOrder.getCustomization().getSugarQuantity());
                notesTxt.setText(Formatter.format(selectedItemOrder.getCustomization().getNotes()));
            }else{
                clearCustomizationFields();
            }
            statusBox.setSelectedItem(selectedItemOrder.getStatus());
        }
    }

    private void clearItemOrderFields() {
        idTxt.setText("");
        menuItemTxt.setText("");
        orderTimeTxt.setText("");
        setEmployeeTxt(null, waitPersonTxt);
        setEmployeeTxt(null, chefTxt);
        clearCustomizationFields();
        statusBox.setEnabled(false);
    }

    private void setEmployeeTxt(Employee employee, JTextField nameTxt) {
        if(employee == null){
            nameTxt.setText(Formatter.format(null));
        }else{
            nameTxt.setText(Formatter.format(employee.getFirstName()));
        }
    }

    private void clearCustomizationFields() {
        saltComboBox.setEnabled(false);
        sugarComboBox.setEnabled(false);
        notesTxt.setText("");
    }

    private void handleUpdateOrder() {
        OrderStatus newStatus = (OrderStatus) statusBox.getSelectedItem();
        if(selectedItemOrder != null){
            selectedItemOrder = ServiceFactory.getItemOrderService().setStatus(selectedItemOrder, newStatus, RestaurantContext.getLoggedInUser());
            fireStatusChange(selectedItemOrder, newStatus);
            updateItemOrderFromFields();
            refreshTableModel();
            updateItemOrderFields();
        }
    }

    private void refreshTableModel() {
        List<ItemOrder> orders = new ArrayList<ItemOrder>();
        if(RestaurantContext.isCustomerMode()){
            orders = itemOrderSvc.getItemOrders(RestaurantContext.getSession());
        }else{
            orders = itemOrderSvc.getCurrentOrders(RestaurantContext.getLoggedInUser());
        }
        if(orders.isEmpty()){
            selectedItemOrder = null;
        }
        tableModel.setItemOrders(orders);
    }

    private void updateSelectedItem() {
        selectedItemOrder = getSelectedItemOrder();
    }

    private void updateItemOrderFromFields() {
        updateCustomizationFromFields();
    }

    private void updateCustomizationFromFields() {
        if(selectedItemOrder.getCustomization() == null){
            selectedItemOrder.setCustomization(new OrderCustomization());
        }
        selectedItemOrder.getCustomization().setSaltQuantity((IngredientQuantity) saltComboBox.getSelectedItem());
        selectedItemOrder.getCustomization().setSugarQuantity((IngredientQuantity) sugarComboBox.getSelectedItem());
        selectedItemOrder.getCustomization().setNotes(notesTxt.getText());
    }

    public void refresh() {
        this.refreshTableModel();
    }

    private void fireStatusChange(ItemOrder selectedItemOrder, OrderStatus orderStatus) {
        for(OrderStatusListener listener : listeners){
            listener.handleOrderStatusChange(selectedItemOrder, orderStatus);
        }
    }
    
    private class ItemOrderTableModel extends AbstractTableModel{
        private List<ItemOrder> itemOrders;
        private ColumnModel<ItemOrder> columns;

        public ItemOrderTableModel(ColumnModel<ItemOrder> columns) {
            this.itemOrders = new ArrayList<ItemOrder>();
            this.columns = columns;
        }

        @Override
        public int getRowCount() {
            return itemOrders.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
           ItemOrder item = itemOrders.get(row);
           Object value = null;
           if(item != null){
              value = columns.getValue(item, col);
           }
           return Formatter.format(value);
        }

        @Override
        public int getColumnCount() {
          return columns.getCount();
        }

        @Override
        public String getColumnName(int col) {
          return columns.getColumnName(col);
        }

        private ItemOrder getItemAtIndex(int selectedIndex) {
            if(selectedIndex == -1) return null;
            return itemOrders.get(selectedIndex);
        }

        public List<ItemOrder> getItemOrders(){
            return itemOrders;
        }

        public void setItemOrders(List<ItemOrder> newOrders){
            this.itemOrders.clear();
            if(newOrders != null){
                this.itemOrders.addAll(newOrders);
            }
            tableModel.fireTableDataChanged();
        }

    }

}
